#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h> //fork
#include <sys/wait.h>
#include <iostream>

//#include <unistd.h> // 获取程序运行路径

using namespace std;

int GetVirus(char *ResultPath)
{
    //char * ToolName = "./watcherAntivirus-x86_64";
    //char * ToolName = const_cast< char* >(argv[1]);

    //cout << "argc=" << argc << endl;


    char ToolName[256] = {0};
    memset(ToolName, 0, sizeof(char)*256);
    strcpy(ToolName, "./watcherAntivirus-x86_64.bin");

    char rstPath[256];
    memset(rstPath, 0, sizeof(rstPath));
    //snprintf(rstPath, 256-1, "../%s/病毒/ret", ResultPath);
   snprintf(rstPath, 256-1, "%s/ret", ResultPath);
	 //snprintf(rstPath, 256-1, "%s/virus/ret", ResultPath);

    pid_t pid;
    pid = fork();

    if(pid < 0)
        return -1;
    else if(pid == 0)
    {
        //cout << "子进程" <<endl;
        //char * arg1[] = {"./watcherAntivirus-x86_64.bin", "-d", "/", "-o", "./result/ret", NULL};
        char * arg1[] = {"./watcherAntivirus-x86_64.bin", "-d", "/", "-o", rstPath, "-i",NULL};
       
        char buffer1[1024] = {0};
        //getcwd(buffer1, 1024);
        //cout << "路径1=" << buffer1 << endl;
		int ret = 0;
        if(chdir("./watcherAntivirus") != 0)
        {
            perror("chdir err");
        }

        char buffer2[1024] = {0};
        //getcwd(buffer2, 1024);
        //cout << "路径2=" << buffer2 << endl;

        if((ret = execve(ToolName, arg1, NULL))<0)
        {
            perror("exec err");
        }
        //cout << ret << endl;
        exit(0);
    }
    else
    {
        //cout << "父进程" << pid << endl;
        int status;
        waitpid(pid, &status, 0);
    }

    return 0;
}
